package com.rongshu.view.home.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.discovery.bean.AuthorDetailBean;
import com.rongshu.view.home.adapter.AuthorListAtAdapter;
import com.rongshu.view.home.contract.SearchAtView;
import com.rongshu.view.home.presenter.SearchAtPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchAtUserActivity extends BaseActivity<SearchAtPresenter> implements SearchAtView {
    @BindView(R.id.search_left_tv)
    EditText search_left_tv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshView;

    private int pageIndex = 1;
    private int pageSize = 10;
    //搜索
    private String msg = "";
    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private AuthorListAtAdapter mAuthorListAtAdapter = null;
    //list
    private List<AuthorDetailBean> articleList = null;
    //user_id
    private int user_id = 0;
    //type
    private int type = 0;//0代表关注的人，1代表搜索的人
    //map
    private Map<String, Object> map = null;

    @Override
    protected int initLayout() {
        return R.layout.activty_search_at_user_view;
    }

    @Override
    protected void initView() {
        //top
        search_left_tv.setHint("请输入关键词...");
        //manager
        manager = new LinearLayoutManager(this);
        //list
        articleList = new ArrayList<>();
        //adapter
        mAuthorListAtAdapter = new AuthorListAtAdapter(this);
        mAuthorListAtAdapter.setAuthorDetailBeans(articleList);
        //reyclerview
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(mAuthorListAtAdapter);
        //presenter
        setPresenter(new SearchAtPresenter(this), this);
        //user_id
        user_id = UserInfoUtils.getUserId();
    }

    @Override
    protected void initData() {
        search_left_tv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    showKeyboard(false);
                    msg = v.getText().toString();
                    pageIndex = 1;
                    type = 1;
                    presenter.getSearchAuthor(msg, pageIndex, pageSize, user_id);
                }
                return false;
            }
        });

        refreshView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                refreshView.resetNoMoreData();
                if (type == 0) {
                    getSetUserRelationships();
                } else {
                    presenter.getSearchAuthor(msg, pageIndex, pageSize, user_id);
                }
            }
        });
        refreshView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                if (type == 0) {
                    getSetUserRelationships();
                } else {
                    presenter.getSearchAuthor(msg, pageIndex, pageSize, user_id);
                }
            }
        });

        //adapter
        mAuthorListAtAdapter.setOnItemClickListener(new AuthorListAtAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent();
                if (mAuthorListAtAdapter != null) {
                    List<AuthorDetailBean> authorDetailBeanList = mAuthorListAtAdapter.getAuthorDetailBeans();
                    intent.putExtra(Constant.INTENT_DATA, authorDetailBeanList.get(position));
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getSetUserRelationships();
    }

    /**
     * 获取我的主页 我的关注 1，我的粉丝 2 ,我的黑名单 3
     */
    private void getSetUserRelationships() {
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("type", 1);
        map.put("page", pageIndex);
        map.put("pageSize", pageSize);
        presenter.getSetUserRelationships(user_id, 1,
                pageIndex, pageSize, Common.encryptionSign(map));
        map.clear();
    }

    @OnClick({R.id.search_right_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_right_tv:
                finish();
                break;
        }
    }

    /**
     * 搜索返回
     */
    @Override
    public void onSearchAtUser(List<AuthorDetailBean> authorDetailBeanList) {
        if (authorDetailBeanList == null) {
            return;
        }
        if (pageIndex == 1) {
            articleList.clear();
        }
        if (refreshView.isLoading()) {
            refreshView.finishLoadmore();
        }
        if (refreshView.isRefreshing()) {
            refreshView.finishRefresh();
        }
        List<AuthorDetailBean> list = authorDetailBeanList;
        articleList.addAll(list);
        mAuthorListAtAdapter.notifyDataSetChanged();
        noDataLayout.setVisibility(articleList.size() == 0 ? View.VISIBLE : View.GONE);
        if (list.size() < pageSize) {
            refreshView.setLoadmoreFinished(true);
        }
    }
}
